from datetime import datetime
from enum import Enum
from typing import Union

from fastapi import Query
from pydantic import BaseModel, Field

from forward.schema_base import EmptyStrToNone
from forward.utils.tools import ToolsUtil


class ArticleListInSortEnum(str, Enum):
    """
    文章列表搜索入参 Enum
    """

    hot = "hot"  # 热门
    new = "new"  # 最新
    default = ""  # 默认


class ArticleBaseOutSchema(BaseModel):
    """
    文章 输出
    """

    id: int
    title: str
    intro: str
    image: str
    visit: int  # 浏览
    collect: bool = Field(default=False)
    created_time: datetime|None = Field(None)  # 创建时间


class ArticleDetailOutSchema(ArticleBaseOutSchema):
    """
    文章 详情 输出
    """

    summary: str
    content: str
    author: str


class ArticleListInSchema(BaseModel):
    """
    文章列表 入参
    """

    cid: Union[int, None, EmptyStrToNone] = Query(default=None)  # 分类ID
    keyword: Union[str, None] = Query(default=None)  # 关键词
    sort: Union[ArticleListInSortEnum, EmptyStrToNone, None] = None


class ArticleDetailInSchema(BaseModel):
    """
    文章详情 入参
    """

    id: Union[int, None, EmptyStrToNone] = Query(default=None)


class ArticleCategoryOutSchema(BaseModel):
    """
    文章分类 输出
    """

    id: int
    name: str = Field(default="")


class ArticleCollectOutSchema(BaseModel):
    """
    文章收藏列表 输出
    """

    id: int
    article_id: int = Field()
    title: str
    image: str
    intro: str
    visit: int  # 浏览
    created_time: datetime|None = Field(None)  # 创建时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class ArticleCollectSchema(BaseModel):
    """
    文章收藏列表 模型
    """

    id: int
    article_id: int
    user_id: int
    created_time: datetime | None = Field(None)  # 创建时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class ArticleSearchOutSchema(BaseModel):
    """
    文章搜索返回
    """

    id: int
    title: str
    image: str
    intro: str
    visit: int  # 浏览
    collect: Union[bool, None] = None  # 收藏
    created_time: datetime = Field()  # 创建时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class ArticleCollectPostInSchema(BaseModel):
    """
    文章加入/取消收藏 入参
    """

    article_id: int = Field(default=0)
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }
